package com.itheima.health.service.impl;

import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.LoginCheck;
import com.itheima.health.pojo.RedisConstant;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MobileService;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.SMSUtils;
import com.itheima.health.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@Service
public class MobileServiceImpl implements MobileService {

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登陆短信验证
     *
     * @param type
     * @param telephone
     * @return
     */
    @Override
    public void checkCode(String type, String telephone) {

        String redisKey = RedisConstant.key1 + type +":" + telephone;
        String code = String.valueOf(ValidateCodeUtils.generateValidateCode(6));

        redisTemplate.boundValueOps(redisKey).set(code,5* 60L, TimeUnit.SECONDS);

       // SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",telephone,code);
    }
    /**
     * 验证码校验
     * @param loginCheck
     * @return
     */
    @Override
    public Boolean loginCheck(LoginCheck loginCheck,String type) {
        String validateCode = loginCheck.getValidateCode();
        String telephone = loginCheck.getTelephone();
        String redisKey = RedisConstant.key1 + type +":" + telephone;
        Object code = redisTemplate.boundValueOps(redisKey).get();
        if ( validateCode != null && validateCode.equals(code)){
            redisTemplate.delete(redisKey);
            return true;
        }else {
            return false;
        }
    }
}
